<?php
header("Content-Type: application/json; charset=UTF-8");
require_once 'db.php';

$action = $_GET['action'] ?? '';
$coachId = $_GET['coach_id'] ?? 0;

if ($action === 'list') {
    $stmt = $conn->prepare("
        SELECT ce.*, u.name, u.avatar 
        FROM coach_evaluate ce
        LEFT JOIN users u ON ce.user_id = u.id
        WHERE ce.coach_id = ? AND ce.status = 1
        ORDER BY ce.create_time DESC
    ");
    $stmt->bind_param("i", $coachId);
    $stmt->execute();
    $result = $stmt->get_result();
    
    $list = [];
    while ($row = $result->fetch_assoc()) {
        $list[] = [
            'id' => $row['id'],
            'rating' => (int)$row['rating'], 
            'content' => $row['content'],
            'create_time' => $row['create_time'],
            'user' => [
                'name' => $row['name'],
                'avatar' => $row['avatar']
            ]
        ];
    }
    
    echo json_encode(['status' => 'success', 'list' => $list]);
}
?>